import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout/index.vue'
import zh from '@/i18n/lang/zh.js'
import en from '@/i18n/lang/en.js'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
// 创建vue-i18n实例i18n
const i18n = new VueI18n({
  // 设置默认语言
  locale: localStorage.getItem('locale') || 'zh', // 语言标识
  // 添加多语言（每一个语言标示对应一个语言文件）
  messages: {
    zh,
    en
  }
})

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue')
  },

  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404.vue')
  },

  // 账号18511111111为超级管理员
  // 账号18522222222为管理员
  // 若roles不加管理员，则管理员账户无法访问相应菜单
  // 类似于权限管理
  {
    path: '/',
    component: Layout,
    redirect: '/dataview',
    children: [
      {
        path: 'dataview',
        component: () => import('@/views/dataview/index.vue'),
        meta: {
          title: i18n.t('index.data'),
          icon: 'el-icon-pie-chart',
          path: '/dataview',
          roles: ['超级管理员', '管理员', '老师', '学生']
        }
      },
      {
        path: '/userlist',
        name: 'userlist',
        component: () => import('@/views/userlist/index.vue'),
        meta: {
          title: i18n.t('index.user'),
          icon: 'el-icon-user',
          path: '/userlist',
          roles: ['超级管理员', '管理员']
        }
      },
      {
        path: '/questions',
        name: 'questions',
        component: () => import('@/views/questions/index.vue'),
        meta: {
          title: i18n.t('index.question'),
          icon: 'el-icon-edit-outline',
          path: '/questions',
          roles: ['超级管理员', '管理员', '老师']
        }
      },
      {
        path: '/business',
        name: 'business',
        component: () => import('@/views/business/index.vue'),
        meta: {
          title: i18n.t('index.enterprise'),
          icon: 'el-icon-office-building',
          path: '/business',
          roles: ['超级管理员', '管理员']
        }
      },
      {
        path: '/subjectlist',
        name: 'subjectlist',
        component: () => import('@/views/SubjectList/index.vue'),
        meta: {
          title: i18n.t('index.subject'),
          icon: 'el-icon-notebook-2',
          path: '/subjectlist',
          roles: ['超级管理员', '管理员', '老师', '学生']
        }
      }
    ]
  },

  // 404
  {
    path: '*',
    redirect: '/404',
    component: () => import('@/views/404.vue')
  }
]
const router = new VueRouter({
  routes
})

/* // 初始化路由
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // 新路由实例matcer，赋值给旧路由实例的matcher，（相当于replaceRouter）
  router.options.routes = []
} */
export default router
